package it.valeandre.omino;

import it.valeandre.omino.utils.Utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

public abstract class BaseFragmentActivity extends BannerActivity implements InterFragmentInterface{

	public final static String KEY_SHOW_DISCLAIMER = "KEY_SHOW_DISCLAIMER";
	protected AlertDialog mConnectionAlertDialog;
	protected Handler mHandler = new Handler();
	protected boolean isActivityUp;

	public void showConnectionAlertDialog() {
		showConnectionAlertDialog(null);
	}

	@Override
	protected void onResume() {
		super.onResume();
		isActivityUp = true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		isActivityUp = false;
	}
	
	protected abstract void refreshCurrentPage();
//	protected abstract void setCurrentFragment();

	public void showConnectionAlertDialogAsync(final DialogInterface.OnClickListener listener) {
		mHandler.post(new Runnable() {

			public void run() {
				showConnectionAlertDialog(listener);

			}
		});

	}

	public void showConnectionAlertDialogAsync() {
		mHandler.post(new Runnable() {

			public void run() {
				showConnectionAlertDialog();

			}
		});
	}
	
	protected void showDisclaimerDialog() {
		AlertDialog dialog;
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.disclaimer_layout, null);
		final CheckBox check = (CheckBox)view.findViewById(R.id.disclaimer_checkbox);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(view).setTitle(R.string.warning).setNegativeButton(R.string.ok, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				if(dialog != null) {
					Utils.saveShowDisclaimer(BaseFragmentActivity.this, !check.isChecked());
					dialog.dismiss();
				}
				
			}
		});
		dialog = builder.create();
		dialog.show();
	}

	public void showAlertDialogAsync(final DialogInterface.OnClickListener listener, final String title, final String message) {
		mHandler.post(new Runnable() {

			public void run() {
				showAlertDialog(listener, null, getResources().getString(R.string.ok), null,  title, message);

			}
		});
	}

	public void showConnectionAlertDialog(DialogInterface.OnClickListener listener) {

		showAlertDialog(listener, null, getResources().getString(R.string.ok), null, getResources().getString(R.string.warning), getResources().getString(R.string.connection_alert));

	}

	
//	public void showTryToRefreshDialog() {
//		showAlertDialog(new OnClickListener() {
//			
//			public void onClick(DialogInterface dialog, int which) {
//				refreshCurrentPage();
//				
//			}
//		}, new OnClickListener() {
//			
//			public void onClick(DialogInterface dialog, int which) {
//				if(mConnectionAlertDialog != null) {
//					mConnectionAlertDialog.dismiss();
//				}
//				
//			}
//		}, getResources().getString(R.string.retry), getResources().getString(R.string.continue), getResources().getString(R.string.warning), getResources().getString(R.string.))
//	}

	private void showAlertDialog(DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener,
			String positiveText, String negativeText, String title, String message) {
		if (isActivityUp) {
			if (positiveListener == null) {
				positiveListener = new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						finish();

					}

				};
			}
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(title).setMessage(message).setPositiveButton(positiveText, positiveListener);
			if(negativeListener != null) {
				builder.setNegativeButton(negativeText, negativeListener);
			}
			if (mConnectionAlertDialog == null) {
				mConnectionAlertDialog = builder.create();
				mConnectionAlertDialog.setCancelable(false);
			}
			if (!mConnectionAlertDialog.isShowing()) {
				try {
					mConnectionAlertDialog.show();
				} catch (Exception e) {
					// Log.w("TAG",
					// "Activity is down, cant show connection alert!");
				}
			}
		}

	}
	
	

}
